/**
 * 初始化部门详情对话框
 */
var ReservationsInfoDlg = {
    reservationsInfoData: {},
    zTreeInstance: null,
    validateFields: {
        filingTime: {
            validators: {
                notEmpty: {
                    message: '建档时间不能为空'
                }
            }
        },
        customerName: {
            validators: {
                notEmpty: {
                    message: '客户姓名不能为空'
                },
                stringLength: {
                    /*长度提示*/
                    max: 20,
                    message: '客户姓名长度不能超过20，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        customerPhone: {
            validators: {
                notEmpty: {
                    message: '电话不能为空'
                },
                stringLength: {
                    /*长度提示*/
                    max: 20,
                    message: '电话长度不能超过20，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        archiving: {
            validators: {
                notEmpty: {
                    message: '建档人不能为空'
                },
                stringLength: {
                    /*长度提示*/
                    max: 20,
                    message: '建档人长度不能超过20，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        qqNumber: {
            validators: {
                stringLength: {
                    /*长度提示*/
                    max: 25,
                    message: 'qq号长度不能超过25，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        wechatNumber: {
            validators: {
                stringLength: {
                    /*长度提示*/
                    max: 45,
                    message: '微信号长度不能超过45，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        otherContact: {
            validators: {
                stringLength: {
                    /*长度提示*/
                    max: 100,
                    message: '其他联系方式长度不能超过100，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        follower: {
            validators: {
                stringLength: {
                    /*长度提示*/
                    max: 25,
                    message: '跟进人长度不能超过25，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        obstaclePoint: {
            validators: {
                stringLength: {
                    /*长度提示*/
                    max: 30000,
                    message: '障碍点长度不能超过3万，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        quarters: {
            validators: {
                stringLength: {
                    /*长度提示*/
                    max: 100,
                    message: '小区长度不能超过100，请重新输入...'
                }/*最后一个没有逗号*/
            }
        },
        customerAge: {
            validators: {
                regexp: {
                    /* 只需加此键值对，包含正则表达式，和提示 */
                    regexp: /^([0-9]\d{0,2}|1000)$/,
                    message: '年龄为 0 ~ 1000 间整数，请重新输入...'
                },
            }
        }

    }
};

/**
 * 清除数据
 */
ReservationsInfoDlg.clearData = function () {
    this.reservationsInfoData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ReservationsInfoDlg.set = function (key, val) {
    this.reservationsInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ReservationsInfoDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
ReservationsInfoDlg.close = function () {
    // parent.layer.close(window.parent.Reservations.layerIndex);
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}


/**
 * 显示部门选择的树
 *
 * @returns
 */
ReservationsInfoDlg.showReservationsSelectTree = function () {
    var pName = $("#pName");
    var pNameOffset = $("#pName").offset();
    $("#parentReservationsMenu").css({
        left: pNameOffset.left + "px",
        top: pNameOffset.top + pName.outerHeight() + "px"
    }).slideDown("fast");

    $("body").bind("mousedown", onBodyDown);
}

/**
 * 隐藏部门选择的树
 */
ReservationsInfoDlg.hideReservationsSelectTree = function () {
    $("#parentReservationsMenu").fadeOut("fast");
    $("body").unbind("mousedown", onBodyDown);// mousedown当鼠标按下就可以触发，不用弹起
}

/**
 * 收集数据
 */
ReservationsInfoDlg.collectData = function () {
    this.set('id').set('customerName').set('customerPhone').set('customerSex').set('customerAge').set('economicAbility')
        .set('qqNumber').set('wechatNumber').set('otherContact').set('country').set('province').set('cities')
        .set('region').set('quarters').set('maritalStatus').set('occupation').set('archiving').set('filingTime')
        .set('entryClerk').set('follower').set('vehicle').set('obstaclePoint').set('oneMedium').set('twoMedium').set('thrMedium')
        .set('oneChannel').set('twoChannel').set('thrChannel').set('appointmentArrivalTime').set('appointmentPayService')
        .set('isLarge').set('accessDisease').set('remarks').set('classification').set('status').set('outpatientName').set('phoneSharShip');
}


/**
 * 验证数据是否为空
 */
ReservationsInfoDlg.validate = function () {
    $('#reservationsInfoForm').data("bootstrapValidator").resetForm();
    $('#reservationsInfoForm').bootstrapValidator('validate');
    return $("#reservationsInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 提交添加预约客户
 */
ReservationsInfoDlg.addSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/reservations/add", function (data) {
        if (data == 1) {
            Feng.success("添加成功!");
            window.parent.Reservations.table.refresh();
            ReservationsInfoDlg.close();
        } else {
            alert("客户电话已存在");
        }
    }, function (data) {
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.reservationsInfoData);
    ajax.start();
}
/*建档，跟进点击*/
ReservationsInfoDlg.getArchiving = function () {
    var index = layer.open({
        type: 2,
        title: '添加建档人',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/to_archiving'
    });
    this.layerIndex = index;
};

ReservationsInfoDlg.getFollower = function () {
    var index = layer.open({
        type: 2,
        title: '添加跟进人',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/to_follower'
    });
    this.layerIndex = index;
};
/**
 * 提交添加补单预约客户
 */
ReservationsInfoDlg.supplement = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/reservations/supplement", function (data) {
        if (data == 1) {
            Feng.success("添加补单成功!");
            window.parent.Reservations.table.refresh();
            ReservationsInfoDlg.close();
        } else {
            alert("客户电话已存在");
        }
    }, function (data) {
        Feng.error("添加补单失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.reservationsInfoData);
    ajax.start();
}

/**
 * 提交建档
 */
ReservationsInfoDlg.editSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/reservations/supplement_update", function (data) {
        if (data == 1) {
            Feng.success("修改成功!");
            window.parent.parent.Reservations.table.refresh();
            $('#cancel', parent.document).click();
        } else {
            alert("客户电话已存在");
        }
    }, function (data) {
        Feng.error("修改失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.reservationsInfoData);
    ajax.start();
}
var seletprovince = $('#provinceValue').val();
var seletcities = $('#citiesValue').val();
var seletregion = $('#regionValue').val();
//初始化数据
ReservationsInfoDlg.initData = function () {
    //状态单选
    var seletecustomerSex = $('#customerSexValue').val();
    if ($('#id').val() != "") {
        $("#customerSex option[value=" + seletecustomerSex + "]").attr("selected", "selected");
    }
    var seletemaritalStatus = $('#maritalStatusValue').val();
    if ($('#id').val() != "") {
        $("#maritalStatus option[value=" + seletemaritalStatus + "]").attr("selected", "selected");
    }
    var seleteisLarge = $('#isLargeValue').val();
    if ($('#id').val() != "") {
        $("#isLarge option[value=" + seleteisLarge + "]").attr("selected", "selected");
    }
    var seletestatus = $('#statusValue').val();
    if ($('#id').val() != "") {
        $("#status option[value=" + seletestatus + "]").attr("selected", "selected");
    }
    var seletephoneSharShip = $('#phoneSharShipValue').val();
    if ($('#id').val() != "") {
        $("#phoneSharShip option[value=" + seletephoneSharShip + "]").attr("selected", "selected");
    }
    var seleteaccessDisease = $('#accessDiseaseValue').val();
    if ($('#id').val() != ""&&seleteaccessDisease!="") {
        $("#accessDisease option[value=" + seleteaccessDisease + "]").attr("selected", "selected");
    }
}

function onBodyDown(event) {
    if (!(event.target.id == "menuBtn" || event.target.id == "parentReservationsMenu" || $(
            event.target).parents("#parentReservationsMenu").length > 0)) {
        ReservationsInfoDlg.hideReservationsSelectTree();
    }
}
var myDate = new Date();
var month;
var day;
if ((myDate.getMonth() + 1) < 10) {
    month = "0" + (myDate.getMonth() + 1);
} else {
    month = "" + (myDate.getMonth() + 1);
}
if (myDate.getDate() < 10) {
    day = "0" + myDate.getDate();
} else {
    day = "" + myDate.getDate();
}
$(function () {
    Feng.initValidator("reservationsInfoForm", ReservationsInfoDlg.validateFields);
    // document.getElementById("filingTime").value = myDate.getFullYear() + "-" + month + "-" + day + " "
    //     + myDate.getHours()+":"+myDate.getMinutes()+":"+myDate.getSeconds();
    ReservationsInfoDlg.initData();
    // $("#province option[value=" + seletprovince + "]").attr("selected", "selected");
    // $("#cities option[value=" + seletcities + "]").attr("selected", "selected");
    // $("#region option[value=" + seletregion + "]").attr("selected", "selected");
    //初始化下拉交通工具列表
    $.post(Feng.ctxPath + "/vehicle/getList", function (data) {
        var txt = '<option value="">--请选择交通工具--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].vehicle_name + '">' + data[i].vehicle_name + '</option>';
        }
        document.getElementById('vehicle').innerHTML = txt;
        //状态单选
        var seletvehicle = $('#vehicleValue').val();
        if ($('#id').val() != "" && seletvehicle != "") {
            $("#vehicle option[value=" + seletvehicle + "]").attr("selected", "selected");
        }
    }, "json");

    //初始化下拉经济能力列表
    $.post(Feng.ctxPath + "/economicAbility/getList", function (data) {
        var txt = '<option value="">--请选择经济能力--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].economic_ability + '">' + data[i].economic_ability + '</option>';
        }
        document.getElementById('economicAbility').innerHTML = txt;
        //状态单选
        var seleteconomicAbility = $('#economicAbilityValue').val();
        if ($('#id').val() != "" && seleteconomicAbility != "") {
            $("#economicAbility option[value=" + seleteconomicAbility + "]").attr("selected", "selected");
        }
    }, "json");

    //初始化下拉经济能力列表
    $.post(Feng.ctxPath + "/occupation/getList", function (data) {
        var txt = '<option value="">--请选择职业--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].occupation + '">' + data[i].occupation + '</option>';
        }
        document.getElementById('occupation').innerHTML = txt;
        //状态单选
        var seleteoccupation = $('#occupationValue').val();
        if ($('#id').val() != "" && seleteoccupation != "") {
            $("#occupation option[value=" + seleteoccupation + "]").attr("selected", "selected");
        }
    }, "json");

    //初始化下拉到店门诊列表
    $.post(Feng.ctxPath + "/outpatientManagement/getList", function (data) {
        var txt = '<option value="">--请选择到店门诊--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].simplename + '">' + data[i].simplename + '</option>';
        }
        document.getElementById('outpatientName').innerHTML = txt;
        //状态单选
        var seleteooutpatientName = $('#outpatientNameValue').val();
        if ($('#id').val() != "" && seleteooutpatientName != "") {
            $("#outpatientName option[value=" + seleteooutpatientName + "]").attr("selected", "selected");
        }
    }, "json");

    //初始化下拉到店门诊列表
    $.post(Feng.ctxPath + "/payService/getList", function (data) {
        var txt = '<option value="">--请选择预约项目--</option>';
        for (var i = 0; i < data.length; i++) {
            txt += '<option value="' + data[i].department + '">' + data[i].department + '</option>';
        }
        document.getElementById('appointmentPayService').innerHTML = txt;
        //状态单选
        var seletepayservicename = $('#payservicenameValue').val();
        if ($('#id').val() != "" && seletepayservicename != "") {
            $("#appointmentPayService option[value=" + seletepayservicename + "]").attr("selected", "selected");
        }
    }, "json");

});
